package it.afs.worklog.application.form.admin;

import it.afs.worklog.Attivita;
import it.afs.worklog.Cliente;
import it.afs.worklog.Commessa;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

/**
 * @author Fanta
 */
public class AttivitaPage extends AdminPage
{
	
	private Model<Commessa> commessaModel;
	
	public AttivitaPage(PageParameters parameters)
	{
		super(parameters);
		
		commessaModel = new Model<Commessa>(getFactory().getDao().getCommessa(parameters.getString("commessa")));
		
		addComponents();
	}

	@Override
	protected void addComponents()
	{
		super.addComponents();
		
		add(new Link<Cliente>("back", new Model<Cliente>(commessaModel.getObject().getCliente()))
		{
			private static final long serialVersionUID = -7888633875577385064L;

			@Override
			public void onClick()
			{
				setResponsePage(CommessePage.class, new PageParameters("cliente=" + getModelObject().getId()));
			}
		});
		
		add(new Label("cliente-commessa", String.format("%s - %s", commessaModel.getObject().getCliente().getNominativo(), commessaModel.getObject().getTitolo())));
		
		LoadableDetachableModel<List<Attivita>> attivitaModel = new LoadableDetachableModel<List<Attivita>>()
		{
			private static final long serialVersionUID = -2951976988431855293L;

			@Override
			protected List<Attivita> load()
			{
				return getFactory().getDao().getAttivita(commessaModel.getObject());
			}
		};
		
		ListView<Attivita> clienti = new ListView<Attivita>("clienti", attivitaModel)
		{
			private static final long serialVersionUID = -2903359362399737379L;

			@Override
			protected void populateItem(ListItem<Attivita> item)
			{
				item.add(new Label("oggetto", new PropertyModel<String>(item.getModel(), "oggetto")));
				item.add(new WebMarkupContainer("attivo", new PropertyModel<Boolean>(item.getModel(), "attivo"))
				{
					private static final long serialVersionUID = -8185397038841287809L;

					@Override
					protected void onConfigure()
					{
						super.onConfigure();
						setVisible((Boolean)this.getDefaultModelObject());
					}
				});
				item.add(new Link<String>("edit", new PropertyModel<String>(item.getModel(), "id"))
				{
					private static final long serialVersionUID = 3116102666539232113L;

					@Override
					public void onClick()
					{
						setResponsePage(AttivitaFormPage.class, new PageParameters("id=" + getModelObject()));
					}
				});
			}
		};
		add(clienti);
		
		add(new Link<Commessa>("add", commessaModel)
		{
			private static final long serialVersionUID = 5124602430209101507L;

			@Override
			public void onClick()
			{
				setResponsePage(AttivitaFormPage.class, new PageParameters("commessa=" + commessaModel.getObject().getId()));
			}
		});
	}
}